from socket import *


def main():
    # 创建udp套接字
    udp_socket = socket(AF_INET, SOCK_DGRAM)
    # 绑定端口 写死端口
    local_addr = ('', 9999)
    udp_socket.bind(local_addr)

    dest_ip = input("请输入目标IP地址：")
    dest_port = int(input("请输入目标端口号："))

    # 发送数据
    send_data = input("请输入要发送的数据：")
    udp_socket.sendto(send_data.encode("utf-8"), (dest_ip, dest_port))

    # 接收数据
    recv_data, addr = udp_socket.recvfrom(1024)
    data = recv_data.decode("utf-8")
    print(data, addr)

    # 关闭套接字
    udp_socket.close()


if __name__ == '__main__':
    main()
